package com.jianggujin.redis;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

import com.jianggujin.redis.component.ConnectionTree;
import com.jianggujin.redis.component.MainMenuBar;
import com.jianggujin.redis.panel.KeyValuePanel;
import com.jianggujin.redis.util.ResourceUtils;
import com.jianggujin.redis.util.SwingUtils;

public class MainFrame extends JFrame {

    private MainMenuBar menuBar;
    private JSplitPane centerPane;
    // 中部左侧组件
    private ConnectionTree connectionTree;

    private KeyValuePanel keyValuePanel;

    private MainFrameHandler handler;

    public MainFrame() {
        handler = new MainFrameHandler(this);
        menuBar = new MainMenuBar();
        menuBar.setMainMenuListener(handler);
        this.setJMenuBar(menuBar);
        connectionTree = new ConnectionTree();
        connectionTree.setConnectionListener(handler);
        keyValuePanel = new KeyValuePanel();
        centerPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, connectionTree, keyValuePanel);
        centerPane.setOneTouchExpandable(true);
        centerPane.setDividerLocation(150);
        this.add(centerPane);
        SwingUtils.resizeFont(this);
        setTitle("RedisClient");
        setIconImage(ResourceUtils.ICON_LOGO.getImage());
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        setSize(700, 600);
        setLocationRelativeTo(null);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                handler.handleQuitApplication();
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                UIManager.setLookAndFeel(NimbusLookAndFeel.class.getName());
            } catch (Exception e) {
            }
            MainFrame mainFrame = new MainFrame();
            mainFrame.setVisible(true);
            mainFrame.handler.handleOpenConnect();
        });
    }

    protected ConnectionTree getConnectionTree() {
        return connectionTree;
    }

    protected KeyValuePanel getKeyValuePanel() {
        return keyValuePanel;
    }

    protected JSplitPane getCenterPane() {
        return centerPane;
    }
}